import { isValid, ScrollView } from "cc";
import { Observable } from "rxjs";

export function CCBindScrollView(scrollView: ScrollView): Observable<{ state: string, target: ScrollView }> {
    return new Observable((observer) => {
        let scrolling = () => {
            observer.next({ state: 'scrolling', target: scrollView });
        }
        let scrollingend = () => {
            observer.next({ state: 'scroll-ended', target: scrollView });
        }
        scrollView.node.on('scrolling', scrolling, this);
        scrollView.node.on('scroll-ended', scrollingend, this);
        return () => {
            if (isValid(scrollView.node)) scrollView.node.off('scrolling', scrolling);
            if (isValid(scrollView.node)) scrollView.node.off('scroll-ended', scrollingend);
        }
    });
};